﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;

namespace LockCrypt {
    /// <summary>
    /// Easter Egg!
    /// </summary>
    public partial class WinFollow : Window {
        public WinFollow() {
            InitializeComponent();
        }

        private void OpenForums(object sender, MouseEventArgs e) {
            Process.Start("http://forums.lime49.com/");
        }

        private void Follow_MouseMove(object sender, MouseEventArgs e) {
            /*Point mousePos = Mouse.GetPosition(this);
            double screenPercentageX = mousePos.X / Width,
                   screenPercentageY = mousePos.Y / Height;
            int leftX = 70,
                leftY = 100;
            double mHorizontalSkew = 1.3,//3.5,
                   mEyeIndependence = 0,// 0.5,
                   mGapFactor = 1.2;//1.5;
            
            double mLeftEyeX = 95,
                   mLeftEyeY = 121,
                   mLeftPupilX = mLeftEyeX,
                   mLeftPupilY = mLeftEyeY;
            double mRightEyeX = 139,
                   mRightEyeY = 122,
                   mRightPupilX = mRightEyeX,
                   mRightPupilY = mRightEyeX;
            double mPupilRadius = 5,
                   mLeftEyeRadius = left.ActualWidth,
                   mRightEyeRadius = right.ActualWidth;

            // calculate the pupil travel radii
            double mLeftPupilTR = mLeftEyeRadius - (int)(mGapFactor * mPupilRadius),
                   mRightPupilTR = mRightEyeRadius - (int)(mGapFactor * mPupilRadius);


            double leftDX = mousePos.X - mLeftEyeX;
            double leftDY = mousePos.Y - mLeftEyeY;
            if(leftDY == 0)
                leftDY = 1; // prevent divide by zero
            double leftDXDY = (double)leftDX / leftDY;
            double leftdy = Math.Sqrt(Math.Pow(mLeftEyeRadius, 2) / (Math.Pow(leftDXDY, 2) + 1));
            if(leftDY < 0) {
                leftdy = -leftdy;
            }
            double leftdx = leftDXDY * leftdy * mHorizontalSkew;

            double rightDX = mousePos.X - mRightEyeX;
            double rightDY = mousePos.Y - mRightEyeY;
            if(rightDY == 0)
                rightDY = 1; // prevent divide by zero
            double rightDXDY = (double)rightDX / rightDY;
            double rightdy = Math.Sqrt(Math.Pow(mRightEyeRadius, 2) / (Math.Pow(rightDXDY, 2) + 1));
            double rightdx = rightDXDY * rightdy * mHorizontalSkew;

            // coordinates for the left pupil
            double leftpdy = Math.Sqrt(Math.Pow(mLeftPupilTR, 2) / (Math.Pow(leftDXDY, 2) + 1));
            if(leftDY < 0) {
                leftpdy = -leftpdy;
            }
            double leftpdx = leftDXDY * leftpdy * (mHorizontalSkew - mGapFactor);

            // coordinates for the right pupil
            double rightpdy = Math.Sqrt(Math.Pow(mRightPupilTR, 2) / (Math.Pow(rightDXDY, 2) + 1));
            if(rightDY < 0) {
                rightpdy = -rightpdy;
            }
            double rightpdx = rightDXDY * rightpdy * (mHorizontalSkew - mGapFactor);

            // adjustments for the pupils
            double avepdx = (rightpdx + leftpdx) / 2;
            double avepdy = (rightpdy + leftpdy) / 2;
            leftpdx = leftpdx + (avepdx - leftpdx) * (1 - mEyeIndependence);
            rightpdx = rightpdx + (avepdx - rightpdx) * (1 - mEyeIndependence);
            leftpdy = leftpdy + (avepdy - leftpdy) * (1 - mEyeIndependence);
            rightpdy = rightpdy + (avepdy - rightpdy) * (1 - mEyeIndependence);

            // new pupil positions
            mLeftPupilX = mLeftEyeX + (int)leftpdx;
            mLeftPupilY = mLeftEyeY + (int)leftpdy;
            mRightPupilX = mRightEyeX + (int)rightpdx;
            mRightPupilY = mRightEyeY + (int)rightpdy;
            Canvas.SetLeft(leftPupil, mLeftPupilX);
            Canvas.SetTop(leftPupil, mLeftPupilY);
            Canvas.SetLeft(rightPupil, mRightPupilX);
            Canvas.SetTop(rightPupil, mRightPupilY);*/

            /*double lOffsetX = left.ActualWidth * ((screenPercentageX > 100 || screenPercentageX < 0) ? 1 : screenPercentageX),
                   lOffsetY = left.ActualHeight * ((screenPercentageY > 100 || screenPercentageY < 0) ? 1 : screenPercentageY);
            Point lPosition = AbsoluteLocation(left);
            double lPupilX = lPosition.X + lOffsetX,
                   lPupilY = lPosition.Y + lOffsetY;
            Canvas.SetLeft(leftPupil, lPupilX);
            Canvas.SetTop(leftPupil, lPupilY);

            double rOffsetX = right.ActualWidth * ((screenPercentageX > 100 || screenPercentageX < 0) ? 1 : screenPercentageX),
                   rOffsetY = right.ActualHeight * ((screenPercentageY > 100 || screenPercentageY < 0) ? 1 : screenPercentageY);
            Point rPosition = AbsoluteLocation(right);
            double rPupilX = rPosition.X + rOffsetX,
                   rPupilY = rPosition.Y + rOffsetY;
            Canvas.SetLeft(rightPupil, rPupilX);
            Canvas.SetTop(rightPupil, rPupilY);

            Console.Write(mousePos + "-"+screenPercentageX+"-"+screenPercentageY+"\t" + lPosition + "\tX:" + lOffsetX + "\t" + lPupilX + "\t\tY:" + lOffsetY + "\t" + lPupilY + "\n");
            */

            /*Point mousePos = this.PointToScreen(Mouse.GetPosition(this));
            double radius = left.ActualWidth/2,
                   diameter = radius * 2;
            Point lPosition = AbsoluteLocation(left);
            Point lCenter = new Point(lPosition.X + radius, lPosition.Y + radius);

            double maxX = lPosition.X + diameter,
                   maxY = lPosition.Y + diameter;
            double screenPercentageX = SystemParameters.PrimaryScreenWidth / (lCenter.X + mousePos.X),
                   screenPercentageY = SystemParameters.PrimaryScreenHeight / (lCenter.Y + mousePos.Y);
            double offsetX = radius * ((screenPercentageX > 100 || screenPercentageX < 0) ? 1 : screenPercentageX),
                   offsetY = radius * ((screenPercentageY > 100 || screenPercentageY < 0) ? 1 : screenPercentageY);
            double lPupilX = lPosition.X - offsetX,
                   lPupilY = lCenter.Y-offsetY;
            Canvas.SetLeft(leftPupil, lPupilX);
            Canvas.SetTop(leftPupil, lPupilY);

            Console.Write(mousePos + "\t" + lCenter + "\tX:" + offsetX + "\t" + lPupilX +"\t\tY:"+ offsetY + "\t" + lPupilY+"\n");*/
        }

        private Point AbsoluteLocation(UIElement control) {
            return control.TransformToAncestor(root).Transform(new Point(0,0));
        }
    }
}
